using System;

namespace FixedPointMath
{
    internal static class AcosLookup
    {
        public const int BitOffset = 17;
        public const int MaxCount = 1 << BitOffset;
        private static Number MaxValue = ((Number)(MaxCount - 1)) / MaxCount;

        /// <summary>
        /// 以Precision为间隔，执行Mathf.Acos，取得角度值
        /// </summary>
        private static Number Precision = Number.One / MaxCount;
        private static Number HalfPrecision = Precision / 2;

        /// <summary>
        /// 获取cos值对应的角度值
        /// [0, 180]
        /// </summary>
        /// <param name="degree"></param>
        /// <returns></returns>
        public static Number Degree(Number value)
        {
            value = MathFP.Clamp(value, Number.NN1, Number.N1);

            if (value > Number.Zero)
            {
                value += HalfPrecision;
                var index = value.value >> (Number.BitOffset - BitOffset);
                return new Number { value = AcosLookupData.s_Table[index] };
            }
            else
            {
                value = -value;
                value += HalfPrecision;
                var index = value.value >> (Number.BitOffset - BitOffset);
                return new Number { value = Number.N180.value - AcosLookupData.s_Table[index] };
            }
        }
    }
}
